﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_post : System.Web.UI.Page
{
    myDataSetTableAdapters.CompetitionsTableAdapter comAdapter = new myDataSetTableAdapters.CompetitionsTableAdapter();
    myDataSetTableAdapters.PostsTableAdapter postAdapter = new myDataSetTableAdapters.PostsTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel2.Visible = true;
            Panel1.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = false;
            PanelResult.Visible = false;
            cmdChooseCom.Visible = false;
            lblInfo.Visible = true;
            displayComGridView();
        }
    }

    protected void displayComGridView()
    {
        myDataSet.CompetitionsDataTable comTable = comAdapter.GetDataConvert();
        GridView1.DataSource = comTable;
        GridView1.DataBind();
    }

    protected void displayPostGridView(int comId)
    {
        myDataSet.PostsDataTable postTable = postAdapter.GetDataConvertByComId(comId);
        GridView2.DataSource = postTable;
        GridView2.DataBind();
    }

    protected void clearCom()
    {
        txtId.Text = "";
        txtTitle.Text = "";
        txtDes.Text = "";
        CKEditor1.Text = "";
        RadioButtonList1.SelectedIndex = -1;
        RadDatePicker1.SelectedDate = null;
        RadDatePicker2.SelectedDate = null;
    }

    protected void clearPost()
    {
        txtPostId.Text = "";
        txtPostAuthor.Text = "";
        txtPostTitle.Text = "";
        CKEditor2.Text = "";
        RadDatePicker3.SelectedDate = null;
    }

    protected void clear1st()
    {
        txt1stId.Text = "";
        txt1stTitle.Text = "";
        txt1stName.Text = "";
        lbl1stError.Visible = false;
    }

    protected void clear2nd()
    {
        txt2ndId.Text = "";
        txt2ndTitle.Text = "";
        txt2ndName.Text = "";
        lbl2ndError.Visible = false;
    }

    protected void clear3rd()
    {
        txt3rdId.Text = "";
        txt3rdTitle.Text = "";
        txt3rdName.Text = "";
        lbl3rdError.Visible = false;
    }

    protected bool checkResultEmpty()
    {
        bool status = true;
        if (txt1stId.Text.Trim() == "")
        {
            lbl1stError.Visible = true;
            status = false;
        }
        else
        {
            lbl1stError.Visible = false;
        }
        if (txt2ndId.Text.Trim() == "")
        {
            lbl2ndError.Visible = true;
            status = false;
        }
        else
        {
            lbl2ndError.Visible = false;
        } 
        if (txt3rdId.Text.Trim() == "")
        {
            lbl3rdError.Visible = true;
            status = false;
        }
        else
        {
            lbl3rdError.Visible = false;
        }               
        return status;
    }

    protected void cmdChooseCom_Click(object sender, EventArgs e)
    {
        displayComGridView();
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        PanelResult.Visible = false;
        cmdChooseCom.Visible = false;
        lblInfo.Visible = true;
        lblTitleCom.Text = "";
        txtIdCom.Text = "";
        clearCom();
    }

    protected void cmdHide_Click(object sender, EventArgs e)
    {
        displayComGridView();
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        PanelResult.Visible = false;
        cmdChooseCom.Visible = false;
        lblInfo.Visible = true;
    }

    protected void getDataComById(int id)
    {
        myDataSet.CompetitionsDataTable comTable = comAdapter.GetDataById(id);
        txtId.Text = comTable[0]["id"].ToString();
        txtTitle.Text = comTable[0]["title"].ToString();
        txtDes.Text = comTable[0]["description"].ToString();
        CKEditor1.Text = comTable[0]["content"].ToString();
        RadioButtonList1.SelectedValue = comTable[0]["is_active"].ToString();
        RadDatePicker1.SelectedDate = DateTime.Parse(comTable.Rows[0]["startDate"].ToString());
        RadDatePicker2.SelectedDate = DateTime.Parse(comTable.Rows[0]["endDate"].ToString());
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        PanelResult.Visible = false;
        cmdChooseCom.Visible = false;
        lblInfo.Visible = false;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        clearCom();
        getDataComById(id);
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        displayComGridView();
    }


    protected void cmdChoose_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = true;
        cmdChooseCom.Visible = true;
        lblTitleCom.Text = txtTitle.Text;
        txtIdCom.Text = txtId.Text;
        clearCom();
        displayPostGridView(int.Parse(txtIdCom.Text.Trim()));

        PanelResult.Visible = true;
        string strSQL = "select posts.id, posts.title, accounts.fullName from posts, accounts where posts.competition_id = " + txtIdCom.Text.Trim() + " and posts.student_id = accounts.id and rank > 0 order by rank asc";
        string strConnection = ConfigurationManager.ConnectionStrings["EnvironmentSurveyConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(strConnection);
        SqlCommand sqlCmd = new SqlCommand(strSQL, conn);
        SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count != 0)
        {
            txt1stId.Text = dt.Rows[0]["id"].ToString();
            txt1stTitle.Text = dt.Rows[0]["title"].ToString();
            txt1stName.Text = dt.Rows[0]["fullName"].ToString();
            txt2ndId.Text = dt.Rows[1]["id"].ToString();
            txt2ndTitle.Text = dt.Rows[1]["title"].ToString();
            txt2ndName.Text = dt.Rows[1]["fullName"].ToString();
            txt3rdId.Text = dt.Rows[2]["id"].ToString();
            txt3rdTitle.Text = dt.Rows[2]["title"].ToString();
            txt3rdName.Text = dt.Rows[2]["fullName"].ToString();
        }
        else
        {
            txt1stId.Text = "";
            txt1stTitle.Text = "";
            txt1stName.Text = "";
            txt2ndId.Text = "";
            txt2ndTitle.Text = "";
            txt2ndName.Text = "";
            txt3rdId.Text = "";
            txt3rdTitle.Text = "";
            txt3rdName.Text = "";
        }
    }

    protected void getDataPostById(int id)
    {
        myDataSet.PostsDataTable postTable = postAdapter.GetDataById(id);
        txtPostId.Text = postTable[0]["id"].ToString();
        txtPostAuthor.Text = postTable[0]["fullName"].ToString();
        txtPostTitle.Text = postTable[0]["title"].ToString();
        CKEditor2.Text = postTable[0]["content"].ToString();
        RadDatePicker3.SelectedDate = DateTime.Parse(postTable.Rows[0]["received_time"].ToString());
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView2.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        Panel4.Visible = true;
        PanelResult.Visible = true;
        cmdChooseCom.Visible = true;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        lbl1stError.Visible = false;
        lbl2ndError.Visible = false;
        lbl3rdError.Visible = false;
        clearPost();
        getDataPostById(id);
    }

    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
        myDataSet.PostsDataTable postTable = postAdapter.GetDataById(id);
        if (postTable.Rows[0]["rank"].ToString() == "0")
        {
            lbl1stError.Visible = false;
            lbl2ndError.Visible = false;
            lbl3rdError.Visible = false;
            postAdapter.DeleteQuery(id);
            clearPost();
            displayPostGridView(int.Parse(txtIdCom.Text.Trim()));
        }
        else
        {
            RegisterStartupScript("as", "<script>alert('Cannot delete post in top 3 winners!')</script>");
        }
    }

    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        displayPostGridView(int.Parse(txtIdCom.Text.Trim()));
    }

    protected void cmdSet1st_Click(object sender, EventArgs e)
    {
        lbl1stError.Visible = false;
        lbl2ndError.Visible = false;
        lbl3rdError.Visible = false;
        txt1stId.Text = txtPostId.Text;
        txt1stTitle.Text = txtPostTitle.Text;
        txt1stName.Text = txtPostAuthor.Text;
        if (txt2ndId.Text == txtPostId.Text)
        {
            clear2nd();
        }
        if (txt3rdId.Text == txtPostId.Text)
        {
            clear3rd();
        }
    }

    protected void cmdSet2nd_Click(object sender, EventArgs e)
    {
        lbl1stError.Visible = false;
        lbl2ndError.Visible = false;
        lbl3rdError.Visible = false;
        txt2ndId.Text = txtPostId.Text;
        txt2ndTitle.Text = txtPostTitle.Text;
        txt2ndName.Text = txtPostAuthor.Text;
        if (txt1stId.Text == txtPostId.Text)
        {
            clear1st();
        }
        if (txt3rdId.Text == txtPostId.Text)
        {
            clear3rd();
        }
    }

    protected void cmdSet3rd_Click(object sender, EventArgs e)
    {
        lbl1stError.Visible = false;
        lbl2ndError.Visible = false;
        lbl3rdError.Visible = false;
        txt3rdId.Text = txtPostId.Text;
        txt3rdTitle.Text = txtPostTitle.Text;
        txt3rdName.Text = txtPostAuthor.Text;
        if (txt1stId.Text == txtPostId.Text)
        {
            clear1st();
        }
        if (txt2ndId.Text == txtPostId.Text)
        {
            clear2nd();
        }
    }

    protected void cmdHidePost_Click(object sender, EventArgs e)
    {
        displayPostGridView(int.Parse(txtIdCom.Text.Trim()));
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = true;
        PanelResult.Visible = true;
        cmdChooseCom.Visible = true;
        lblInfo.Visible = false;
    }

    protected void cmdClear_Click(object sender, EventArgs e)
    {
        clear1st();
        clear2nd();
        clear3rd();
    }


    protected void cmdUpdateResult_Click(object sender, EventArgs e)
    {
        if (checkResultEmpty())
        {
            int competition_id = int.Parse(txtIdCom.Text.Trim());
            postAdapter.ClearRankByComIdQuery(competition_id);
            postAdapter.UpdateRankQuery(1, int.Parse(txt1stId.Text.Trim()));
            postAdapter.UpdateRankQuery(2, int.Parse(txt2ndId.Text.Trim()));
            postAdapter.UpdateRankQuery(3, int.Parse(txt3rdId.Text.Trim()));
            comAdapter.UpdateHasResultQuery(competition_id);
            displayPostGridView(competition_id);
        }
    }
}
